﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BSCMFF.Core.DataAccessLayer;
using Telerik.Web.UI;
using System.Drawing;

namespace BSCMFF.Web.UserControls.Details
{
    /// <summary>
    /// Evaluation details.
    /// </summary>
    /// <remarks></remarks>
    public partial class EvaluationDetails : System.Web.UI.UserControl
    {
        /// <summary>
        /// evaluation period.
        /// </summary>
        private Evaluation_Period _eval;// = new Evaluation();

        /// <summary>
        /// Gets or sets the evalut.
        /// </summary>
        /// <value>The evalut.</value>
        /// <remarks></remarks>
        public Evaluation_Period Evalut
        {
            get
            {
                if (_eval == null)
                {
                    _eval = new Evaluation_Period();
                    int tempID = 0;
                    Int32.TryParse(HiddenFieldID.Value, out tempID);
                    _eval.ID = tempID;
                    _eval.Name = RadTextBoxName.Text;
                    _eval.Description = RadTextBoxDescription.Text;
                    if (RadDatePickerStartDate.SelectedDate != null)
                    {
                        _eval.StartDate = (DateTime)RadDatePickerStartDate.SelectedDate;
                    }
                    if (RadDatePickerEndDate.SelectedDate != null)
                    {
                        _eval.EndDate = (DateTime)RadDatePickerEndDate.SelectedDate;
                    }
                    return _eval;
                }
                else
                {
                    return _eval;
                }
            }
            set
            {
                _eval = value;
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is edit mode.
        /// </summary>
        /// <value><c>true</c> if this instance is edit mode; otherwise, <c>false</c>.</value>
        /// <remarks></remarks>
        public bool IsEditMode { get; set; }


        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack && !IsEditMode) //preview mode
            {
                foreach (Control c in this.Controls)
                {
                    if (c.GetType().Name == "RadTextBox")
                    {
                        c.Visible=false;
                        Label l = (Label)this.FindControl(c.ID.Replace("RadTextBox", "LabelCurrent"));
                        l.Visible=true;
                    }
                    if (c.GetType().Name == "RadDatePicker")
                    {
                        c.Visible=false;
                        //RadDatePicker
                        Label l = (Label)this.FindControl(c.ID.Replace("RadDatePicker", "LabelCurrent"));
                        if (l.Text != "")
                        {
                            l.Text = DateTime.Parse(l.Text).ToShortDateString();
                        }
                        l.Visible=true;
                    }
                }
                PlaceHolderButtons.Visible = false;
            }

            if (!IsPostBack)
            {
                CheckEndDate();
            }
        }

        /// <summary>
        /// Checks the end date.
        /// </summary>
        /// <remarks></remarks>
        private void CheckEndDate()
        {
            if (RadDatePickerStartDate.SelectedDate != null && RadDatePickerEndDate.SelectedDate != null)
            {
                DateTime start = ((DateTime)RadDatePickerStartDate.SelectedDate);
                DateTime end = ((DateTime)RadDatePickerEndDate.SelectedDate);

                if (end.AddMonths(-1) == start)
                {
                    RadButtonMonth.BackColor = Color.LightGreen;
                }
            }
        }

        /// <summary>
        /// Handles the Click event of the RadButtonMonth control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        protected void RadButtonMonth_Click(object sender, EventArgs e)
        {
            if (RadDatePickerStartDate.SelectedDate != null)
            {
                RadDatePickerEndDate.SelectedDate = RadDatePickerStartDate.SelectedDate.Value.AddMonths(1);
                ChangeButtonColor("month");
            }
        }

        /// <summary>
        /// Changes the color of the button according to interval chosen.
        /// </summary>
        /// <param name="interval">The interval.</param>
        /// <remarks></remarks>
        private void ChangeButtonColor(string interval)
        {
            switch (interval)
            {
                case "month": RadButtonMonth.BackColor = Color.LightGreen; 
                              RadButtonQuarter.BackColor = Color.Transparent;
                              RadButtonHalfYear.BackColor = Color.Transparent;
                              RadButtonYear.BackColor = Color.Transparent;
                              break;
                case "quarter": RadButtonMonth.BackColor = Color.Transparent;
                              RadButtonQuarter.BackColor = Color.LightGreen;
                                RadButtonHalfYear.BackColor = Color.Transparent;
                                RadButtonYear.BackColor = Color.Transparent;
                                break;
                case "halfyear": RadButtonMonth.BackColor = Color.Transparent;
                                 RadButtonQuarter.BackColor = Color.Transparent;
                                 RadButtonHalfYear.BackColor = Color.LightGreen;
                                 RadButtonYear.BackColor = Color.Transparent;
                                 break;
                case "year": RadButtonMonth.BackColor = Color.Transparent;
                             RadButtonQuarter.BackColor = Color.Transparent;
                             RadButtonHalfYear.BackColor = Color.Transparent;
                             RadButtonYear.BackColor = Color.LightGreen;
                             break;
                default: RadButtonMonth.BackColor = Color.Transparent;
                         RadButtonQuarter.BackColor = Color.Transparent;
                         RadButtonHalfYear.BackColor = Color.Transparent;
                         RadButtonYear.BackColor = Color.Transparent;
                         break; 
            }
        }

        /// <summary>
        /// Handles the Click event of the RadButtonQuarter control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        protected void RadButtonQuarter_Click(object sender, EventArgs e)
        {
            if (RadDatePickerStartDate.SelectedDate != null)
            {
                RadDatePickerEndDate.SelectedDate = RadDatePickerStartDate.SelectedDate.Value.AddMonths(3);
                ChangeButtonColor("quarter");
            }
        }

        /// <summary>
        /// Handles the Click event of the RadButtonHalfYear control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        protected void RadButtonHalfYear_Click(object sender, EventArgs e)
        {
            if (RadDatePickerStartDate.SelectedDate != null)
            {
                RadDatePickerEndDate.SelectedDate = RadDatePickerStartDate.SelectedDate.Value.AddMonths(6);
                ChangeButtonColor("halfyear");
            }
        }

        /// <summary>
        /// Handles the Click event of the RadButtonYear control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        protected void RadButtonYear_Click(object sender, EventArgs e)
        {
            if (RadDatePickerStartDate.SelectedDate != null)
            {
                RadDatePickerEndDate.SelectedDate = RadDatePickerStartDate.SelectedDate.Value.AddYears(1);
                ChangeButtonColor("year");
            }
        }

        /// <summary>
        /// Handles the SelectedDateChanged event of the RadDatePickerStartDate control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Telerik.Web.UI.Calendar.SelectedDateChangedEventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        protected void RadDatePickerStartDate_SelectedDateChanged(object sender, Telerik.Web.UI.Calendar.SelectedDateChangedEventArgs e)
        {
            CheckEndDate();
        }
    }
}